package com.charges.model.service.impl;

import com.charges.model.dto.BenefitDTO;
import com.charges.model.entity.BenefitDO;
import com.charges.model.query.BenefitQuery;
import com.charges.model.repository.BenefitRepository;
import com.charges.model.service.BenefitService;
import java.util.List;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 权益服务实现类
 * @author admin
 */
@Service
public class BenefitServiceImpl implements BenefitService {

    private static final Logger logger = LoggerFactory.getLogger(BenefitServiceImpl.class);

    private final BenefitRepository benefitRepository;

    public BenefitServiceImpl(BenefitRepository benefitRepository) {
        this.benefitRepository = benefitRepository;
    }

    @Override
    @Transactional(readOnly = true)
    public List<BenefitDTO> findAvailableBenefits(BenefitQuery query) {
        logger.info("查询可用权益列表，入参：{}", query);

        // 通过第三方接口查询商品和充值价格
        List<BenefitDO> benefits = benefitRepository.findByStatusAndCode(BenefitDO.Status.available, query.getCode());

        return benefits.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    private BenefitDTO convertToDTO(BenefitDO benefitDO) {
        BenefitDTO benefitDTO = new BenefitDTO();
        BeanUtils.copyProperties(benefitDO, benefitDTO);
        return benefitDTO;
    }
}